﻿using System;

using Norm;

using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.DataAccess;

namespace Fuse8.DomainFramework.MongoDbWrapper
{
    public class MongoDbUnitOfWork<T> : IUnitOfWork<T>, IDisposable where T : class, IEntityObject
    {
        #region Fields

        private bool _isDisposed = false;

        #endregion

        #region Properties

        /// <summary>
        /// 
        /// </summary>
        public virtual IMongo CurrentDb
        {
            get;
            set;
        } 

        #endregion

        #region Constructor

        /// <summary>
        /// 
        /// </summary>
        public MongoDbUnitOfWork()
        { } 

        #endregion

        #region IDisposable Members

        /// <summary>
        /// 
        /// </summary>
        ~MongoDbUnitOfWork()
        {
            Dispose(false);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                if (disposing)
                {
                    if (CurrentDb != null)
                    {
                        CurrentDb.Dispose();
                    }
                }
            }

            _isDisposed = true;
        }
        
        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            Dispose(true);

            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
